package com.suneast.suneaststore.modules.personal.trace.adapter;

import android.support.annotation.Nullable;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.suneast.suneaststore.R;
import com.suneast.suneaststore.bean.personal.trace.TraceBean;
import com.suneast.suneaststore.modules.personal.trace.event.TraceSelectEvent;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

public class TraceItemAdapter extends BaseQuickAdapter<TraceBean, BaseViewHolder> {

    private boolean showCheckBox;

    private RequestOptions requestOptions;

    public TraceItemAdapter(int layoutResId, @Nullable List<TraceBean> data) {
        super(layoutResId, data);
        requestOptions = new RequestOptions()
                .placeholder(R.drawable.ic_square);
    }

    @Override
    protected void convert(BaseViewHolder helper, TraceBean item) {//

        Glide.with(helper.itemView.getContext()).load(item.getUrl()).apply(requestOptions)
                .into((ImageView) helper.getView(R.id.img_goods_trace));
        CheckBox traceSelectCb = helper.getView(R.id.cb_trace_select);
        traceSelectCb.setOnCheckedChangeListener((CompoundButton buttonView, boolean isChecked) -> {
            EventBus.getDefault().post(new TraceSelectEvent(item, isChecked));
        });

        traceSelectCb.setVisibility(showCheckBox ? View.VISIBLE : View.GONE);
    }

    public void setShowCheckBox(boolean showCheckBox) {
        this.showCheckBox = showCheckBox;
    }
}
